

#import "LivePusherConfigViewController.h"
#import "AlivcParamTableViewCell.h"
#import "AlivcParamModel.h"
#import "LivePusherViewController.h"
#import "AlivcUIConfig.h"
#import <AlivcLivePusher/AlivcLivePusherHeader.h>


@interface LivePusherConfigViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *paramTableView;
@property (nonatomic, strong) UIButton *publisherButton;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isUseAsync; // 是否使用异步接口
@property (nonatomic, assign) BOOL isUseWatermark; // 是否使用水印长
@property (nonatomic, copy) NSString *authKey; // 测试鉴权，账号key


@property (nonatomic, strong) AlivcLivePushConfig *pushConfig;

@property (nonatomic, assign) BOOL isUserMainStream;
@property (nonatomic, assign) BOOL isUserMixStream;

@property (nonatomic, assign) BOOL isKeyboardShow;
@property (nonatomic, assign) CGRect tableViewFrame;

@property (nonatomic, copy) NSString *playUrl;
@property (nonatomic, assign) BOOL isSetChannelInfo;
@property (nonatomic, copy) NSString *imagPath;

@property (weak, nonatomic) IBOutlet UILabel *channelInfoTitle;
@property (weak, nonatomic) IBOutlet UILabel *channelInfoModel1Name;
@property (weak, nonatomic) IBOutlet UILabel *channelInfoModel1Height;
@property (weak, nonatomic) IBOutlet UILabel *channelInfoModel1Weight;
@property (weak, nonatomic) IBOutlet UILabel *channelInfoModel2Name;
@property (weak, nonatomic) IBOutlet UILabel *channelInfoModel2Height;
@property (weak, nonatomic) IBOutlet UILabel *channelInfoModel2Weight;
@property (weak, nonatomic) IBOutlet UILabel *channelInfoRemark;
@property (strong, nonatomic) IBOutlet UIView *channelInfoView;

@end

@implementation LivePusherConfigViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupParamData];
    [self setupSubviews];
    [self setupChannelInfo];
    //    [self addKeyboardNoti];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = false;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//根据房间信息生成水印
-(void)setupChannelInfo{
    [[NSBundle mainBundle] loadNibNamed:@"AnchorInfoView"owner:self options:nil];
    if (self.loginDic[@"model1Name"]||self.loginDic[@"model1Height"]||self.loginDic[@"model1Height"]||self.loginDic[@"model2Name"]||self.loginDic[@"model2Height"]||self.loginDic[@"model2Weight"]||self.loginDic[@"remark"]) {
        self.isSetChannelInfo=YES;
    }
    if (self.loginDic[@"model1Name"]){
        self.isSetChannelInfo=YES;
        self.channelInfoModel1Name.text=self.loginDic[@"model1Name"];
    }
    if (self.loginDic[@"model1Height"]){
        self.isSetChannelInfo=YES;
        self.channelInfoModel1Height.text=[NSString stringWithFormat:@"身高：%@",self.loginDic[@"model1Height"]];
    }
    if (self.loginDic[@"model1Weight"]){
        self.isSetChannelInfo=YES;
        self.channelInfoModel1Weight.text=[NSString stringWithFormat:@"体重：%@",self.loginDic[@"model1Weight"]];
    }
    if (self.loginDic[@"model2Name"]){
         self.isSetChannelInfo=YES;
        self.channelInfoModel2Name.text=self.loginDic[@"model2Name"];
    }
    if (self.loginDic[@"model2Height"]){
        self.isSetChannelInfo=YES;
        self.channelInfoModel2Height.text=[NSString stringWithFormat:@"身高：%@",self.loginDic[@"model2Height"]];
    }
    if (self.loginDic[@"model2Weight"]){
        self.isSetChannelInfo=YES;
        self.channelInfoModel1Weight.text=[NSString stringWithFormat:@"体重：%@",self.loginDic[@"model2Weight"]];
    }
    if (self.loginDic[@"remark"]){
        self.isSetChannelInfo=YES;
        self.channelInfoRemark.text=self.loginDic[@"remark"];
    }
    if (self.isSetChannelInfo) {
        self.channelInfoTitle.text=@"房间信息";
    }
    self.channelInfoView.layer.allowsEdgeAntialiasing=YES;
    self.channelInfoView.layer.shouldRasterize=YES;
    UIImage *image=[self snapshotWithView:self.channelInfoView];
    self.imagPath=[self getImagePath:image];
}
#pragma mark - UI
- (void)setupSubviews {
    
    self.view.backgroundColor = [AlivcUIConfig shared].kAVCBackgroundColor;
    self.navigationItem.title = NSLocalizedString(@"pusher_setting", nil);
    
    CGFloat retractX = 10;
    CGFloat viewWidth = AlivcScreenWidth - retractX * 2;
    
    
    self.publisherButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.publisherButton.frame = CGRectMake(0, 0, 150, 50);
    self.publisherButton.center = CGPointMake(AlivcScreenWidth / 2, AlivcScreenHeight -64);
    self.publisherButton.backgroundColor = [AlivcUIConfig shared].kAVCThemeColor;
    [self.publisherButton setTitle:NSLocalizedString(@"start_button", nil) forState:(UIControlStateNormal)];
    self.publisherButton.layer.masksToBounds = YES;
    self.publisherButton.layer.cornerRadius = 10;
    [self.publisherButton addTarget:self action:@selector(publiherButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.paramTableView = [[UITableView alloc] init];
    self.paramTableView.frame = CGRectMake(retractX,
                                           5,
                                           viewWidth,
                                           CGRectGetMinY(self.publisherButton.frame)-10);
    self.paramTableView.delegate = (id)self;
    self.paramTableView.dataSource = (id)self;
    self.paramTableView.separatorStyle = NO;
    self.paramTableView.backgroundColor = [AlivcUIConfig shared].kAVCBackgroundColor;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.paramTableView reloadData];
        [self updateBitrateAndFPSCell];
    });
    
    
    [self.view addSubview:self.publisherButton];
    [self.view addSubview:self.paramTableView];
    self.isKeyboardShow = false;
    self.tableViewFrame = self.paramTableView.frame;
    
}



#pragma mark - Data
- (void)setupParamData {
    self.isUseWatermark = NO;
    self.isUseAsync = YES;
    
    self.pushConfig = [[AlivcLivePushConfig alloc] init];
    
    AlivcParamModel *resolutionModel = [[AlivcParamModel alloc] init];
    resolutionModel.title = NSLocalizedString(@"resolution_label", nil);
    resolutionModel.placeHolder = @"540P";
    resolutionModel.infoText = @"540P";
    resolutionModel.defaultValue = 4.0/6.0;
    resolutionModel.reuseId = AlivcParamModelReuseCellSlider;
    resolutionModel.sliderBlock = ^(int value){
        self.pushConfig.resolution = value;
        [self updateBitrateAndFPSCell];
    };
    
    AlivcParamModel *targetBitrateModel = [[AlivcParamModel alloc] init];
    targetBitrateModel.title = NSLocalizedString(@"target_bitrate", nil);
    targetBitrateModel.placeHolder = @"800";
    targetBitrateModel.infoText = @"Kbps";
    targetBitrateModel.reuseId = AlivcParamModelReuseCellInput;
    targetBitrateModel.valueBlock = ^(int value) {
        self.pushConfig.targetVideoBitrate = value;
    };
    
    AlivcParamModel *minBitrateModel = [[AlivcParamModel alloc] init];
    minBitrateModel.title = NSLocalizedString(@"min_bitrate", nil);
    minBitrateModel.placeHolder = @"200";
    minBitrateModel.infoText = @"Kbps";
    minBitrateModel.reuseId = AlivcParamModelReuseCellInput;
    minBitrateModel.valueBlock = ^(int value) {
        self.pushConfig.minVideoBitrate = value;
    };
    
    AlivcParamModel *initBitrateModel = [[AlivcParamModel alloc] init];
    initBitrateModel.title = NSLocalizedString(@"initial_bitrate", nil);
    initBitrateModel.placeHolder = @"800";
    initBitrateModel.infoText = @"Kbps";
    initBitrateModel.reuseId = AlivcParamModelReuseCellInput;
    initBitrateModel.valueBlock = ^(int value) {
        self.pushConfig.initialVideoBitrate = value;
    };
    
    AlivcParamModel *audioBitrateModel = [[AlivcParamModel alloc] init];
    audioBitrateModel.title = NSLocalizedString(@"audio_bitrate", nil);
    audioBitrateModel.placeHolder = @"64";
    audioBitrateModel.infoText = @"Kbps";
    audioBitrateModel.reuseId = AlivcParamModelReuseCellInput;
    audioBitrateModel.valueBlock = ^(int value) {
        self.pushConfig.audioBitrate = value;
    };
    
    AlivcParamModel *audioSampelModel = [[AlivcParamModel alloc] init];
    audioSampelModel.title = NSLocalizedString(@"audio_sampling_rate", nil);
    audioSampelModel.placeHolder = @"32kHz";
    audioSampelModel.infoText = @"32kHz";
    audioSampelModel.defaultValue = 1.0/2.0;
    audioSampelModel.reuseId = AlivcParamModelReuseCellSlider;
    audioSampelModel.sliderBlock = ^(int value) {
        self.pushConfig.audioSampleRate = value;
    };
    
    AlivcParamModel *fpsModel = [[AlivcParamModel alloc] init];
    fpsModel.title = NSLocalizedString(@"captrue_fps", nil);
    fpsModel.segmentTitleArray = @[@"8",@"10",@"12",@"15",@"20",@"25",@"30"];
    fpsModel.defaultValue = 4;
    fpsModel.reuseId = AlivcParamModelReuseCellSegment;
    fpsModel.segmentBlock = ^(int value) {
        self.pushConfig.fps = value;
    };
    
    AlivcParamModel *minFPSModel = [[AlivcParamModel alloc] init];
    minFPSModel.title = NSLocalizedString(@"min_fps", nil);
    minFPSModel.segmentTitleArray = @[@"8",@"10",@"12",@"15",@"20",@"25",@"30"];
    minFPSModel.defaultValue = 0;
    minFPSModel.reuseId = AlivcParamModelReuseCellSegment;
    minFPSModel.segmentBlock = ^(int value) {
        self.pushConfig.minFps = value;
    };
    
    AlivcParamModel *gopModel = [[AlivcParamModel alloc] init];
    gopModel.title = NSLocalizedString(@"keyframe_interval", nil);
    gopModel.segmentTitleArray = @[@"1s",@"2s",@"3s",@"4s",@"5s"];
    gopModel.defaultValue = 1.0;
    gopModel.reuseId = AlivcParamModelReuseCellSegment;
    gopModel.segmentBlock = ^(int value) {
        self.pushConfig.videoEncodeGop = value;
    };
    
    AlivcParamModel *reconnectDurationModel = [[AlivcParamModel alloc] init];
    reconnectDurationModel.title = NSLocalizedString(@"reconnect_duration", nil);
    reconnectDurationModel.placeHolder = @"1000";
    reconnectDurationModel.infoText = @"ms";
    reconnectDurationModel.reuseId = AlivcParamModelReuseCellInput;
    reconnectDurationModel.valueBlock = ^(int value) {
        self.pushConfig.connectRetryInterval = value;
    };
    
    AlivcParamModel *reconnectTimeModel = [[AlivcParamModel alloc] init];
    reconnectTimeModel.title = NSLocalizedString(@"reconnect_times", nil);
    reconnectTimeModel.placeHolder = @"5";
    reconnectTimeModel.infoText = @"次";
    reconnectTimeModel.reuseId = AlivcParamModelReuseCellInput;
    reconnectTimeModel.valueBlock = ^(int value) {
        self.pushConfig.connectRetryCount = value;
    };
    
    AlivcParamModel *orientationModel = [[AlivcParamModel alloc] init];
    orientationModel.title = NSLocalizedString(@"landscape_model", nil);
    orientationModel.segmentTitleArray = @[@"Portrait",@"HomeLeft",@"HomeRight"];
    orientationModel.defaultValue = 0;
    orientationModel.reuseId = AlivcParamModelReuseCellSegment;
    orientationModel.segmentBlock = ^(int value) {
        self.pushConfig.orientation = value;
        
        if(self.pushConfig.pauseImg) {
            if(self.pushConfig.orientation == AlivcLivePushOrientationPortrait) {
                self.pushConfig.pauseImg = [UIImage imageNamed:@"background_push.png"];
            } else{
                self.pushConfig.pauseImg = [UIImage imageNamed:@"background_push_land.png"];
            }
        }
        
        if(self.pushConfig.networkPoorImg) {
            if(self.pushConfig.orientation == AlivcLivePushOrientationPortrait) {
                self.pushConfig.networkPoorImg = [UIImage imageNamed:@"poor_network.png"];
            } else{
                self.pushConfig.networkPoorImg = [UIImage imageNamed:@"poor_network_land.png"];
            }
        }
        
    };
    
    AlivcParamModel *audioChannelModel = [[AlivcParamModel alloc] init];
    audioChannelModel.title = NSLocalizedString(@"sound_track", nil);
    audioChannelModel.segmentTitleArray = @[NSLocalizedString(@"single_track", nil),NSLocalizedString(@"dual_track", nil)];
    audioChannelModel.defaultValue = 1.0;
    audioChannelModel.reuseId = AlivcParamModelReuseCellSegment;
    audioChannelModel.segmentBlock = ^(int value) {
        self.pushConfig.audioChannel = value;
    };
    
    AlivcParamModel *audioProfileModel = [[AlivcParamModel alloc] init];
    audioProfileModel.title = NSLocalizedString(@"audio_profile", nil);
    audioProfileModel.segmentTitleArray = @[@"AAC_LC",@"HE_AAC",@"HEAAC_V2",@"AAC_LD"];
    audioProfileModel.defaultValue = 0;
    audioProfileModel.reuseId = AlivcParamModelReuseCellSegment;
    audioProfileModel.segmentBlock = ^(int value) {
        self.pushConfig.audioEncoderProfile = value;
    };
    
    AlivcParamModel *mirrorModel = [[AlivcParamModel alloc] init];
    mirrorModel.title = NSLocalizedString(@"push_mirror", nil);
    mirrorModel.defaultValue = 0;
    mirrorModel.titleAppose = NSLocalizedString(@"preview_mirror", nil);
    mirrorModel.defaultValueAppose = 0;
    mirrorModel.reuseId = AlivcParamModelReuseCellSwitch;
    mirrorModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            self.pushConfig.pushMirror = open?true:false;
        } else {
            self.pushConfig.previewMirror = open?true:false;
        }
    };
    
    
    AlivcParamModel *audiOnly_encodeModeModel = [[AlivcParamModel alloc] init];
    audiOnly_encodeModeModel.title = NSLocalizedString(@"audio_only_push_streaming", nil);
    audiOnly_encodeModeModel.defaultValue = 0;
    audiOnly_encodeModeModel.titleAppose = NSLocalizedString(@"hardware_encode", nil);
    audiOnly_encodeModeModel.defaultValueAppose = 1.0;
    audiOnly_encodeModeModel.reuseId = AlivcParamModelReuseCellSwitch;
    audiOnly_encodeModeModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            self.pushConfig.audioOnly = open?true:false;
        } else {
            self.pushConfig.videoEncoderMode = open?AlivcLivePushVideoEncoderModeHard:AlivcLivePushVideoEncoderModeSoft;
        }
    };
    
    AlivcParamModel *autoFocus_FlashModel = [[AlivcParamModel alloc] init];
    autoFocus_FlashModel.title = NSLocalizedString(@"auto_focus", nil);
    autoFocus_FlashModel.defaultValue = 1.0;
    autoFocus_FlashModel.titleAppose = NSLocalizedString(@"flash", nil);
    autoFocus_FlashModel.defaultValueAppose = 0;
    autoFocus_FlashModel.reuseId = AlivcParamModelReuseCellSwitch;
    autoFocus_FlashModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            self.pushConfig.autoFocus = open?true:false;
        } else {
            self.pushConfig.flash = open?true:false;
        }
    };
    
    AlivcParamModel *beauty_cameraTypeModel = [[AlivcParamModel alloc] init];
    beauty_cameraTypeModel.title = NSLocalizedString(@"beauty_button", nil);
    beauty_cameraTypeModel.defaultValue = 1.0;
    beauty_cameraTypeModel.titleAppose = NSLocalizedString(@"front_camera", nil);
    beauty_cameraTypeModel.defaultValueAppose = 1.0;
    beauty_cameraTypeModel.reuseId = AlivcParamModelReuseCellSwitch;
    beauty_cameraTypeModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            self.pushConfig.beautyOn = open?true:false;
        } else {
            self.pushConfig.cameraType = open?AlivcLivePushCameraTypeFront:AlivcLivePushCameraTypeBack;
        }
    };
    
    AlivcParamModel *autoBitrate_resolutionModel = [[AlivcParamModel alloc] init];
    autoBitrate_resolutionModel.title = NSLocalizedString(@"auto_bitrate", nil);
    autoBitrate_resolutionModel.defaultValue = 1.0;
    autoBitrate_resolutionModel.titleAppose = NSLocalizedString(@"auto_resolution", nil);
    autoBitrate_resolutionModel.defaultValueAppose = 0;
    autoBitrate_resolutionModel.reuseId = AlivcParamModelReuseCellSwitch;
    autoBitrate_resolutionModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            self.pushConfig.enableAutoBitrate = open?true:false;
        } else {
            self.pushConfig.enableAutoResolution = open?true:false;
        }
    };
    
    AlivcParamModel *asyncModel = [[AlivcParamModel alloc] init];
    asyncModel.title = NSLocalizedString(@"asynchronous_interface", nil);
    asyncModel.reuseId = AlivcParamModelReuseCellSwitch;
    asyncModel.defaultValue = 1.0;
    asyncModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            self.isUseAsync = open;
        }
    };
    
    AlivcParamModel *watermarkModel = [[AlivcParamModel alloc] init];
    watermarkModel.title = NSLocalizedString(@"watermark", nil);
    watermarkModel.reuseId = AlivcParamModelReuseCellSwitchButton;
    watermarkModel.defaultValue = 0.0;
    watermarkModel.switchBlock = ^(int index, BOOL open) {
        self.isUseWatermark = open;
    };
    
   
    
    AlivcParamModel *videoOnly_audioHardwareEncodeModel = [[AlivcParamModel alloc] init];
    videoOnly_audioHardwareEncodeModel.title = NSLocalizedString(@"video_only_push_streaming", nil);
    videoOnly_audioHardwareEncodeModel.defaultValue = 0.0;
    videoOnly_audioHardwareEncodeModel.titleAppose = NSLocalizedString(@"audio_hardware_encode", nil);
    videoOnly_audioHardwareEncodeModel.defaultValueAppose = 1.0;
    
    videoOnly_audioHardwareEncodeModel.reuseId = AlivcParamModelReuseCellSwitch;
    videoOnly_audioHardwareEncodeModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            self.pushConfig.videoOnly = open?true:false;
        } else {
            self.pushConfig.audioEncoderMode = open?AlivcLivePushAudioEncoderModeHard:AlivcLivePushAudioEncoderModeSoft;
        }
    };
    
    AlivcParamModel *backgroundImage_networkWeakImageModel = [[AlivcParamModel alloc] init];
    backgroundImage_networkWeakImageModel.title = NSLocalizedString(@"background_image", nil);
    backgroundImage_networkWeakImageModel.defaultValue = 1.0;
    backgroundImage_networkWeakImageModel.titleAppose = NSLocalizedString(@"network_weak_image", nil);
    backgroundImage_networkWeakImageModel.defaultValueAppose = 1.0;
    
    backgroundImage_networkWeakImageModel.reuseId = AlivcParamModelReuseCellSwitch;
    backgroundImage_networkWeakImageModel.switchBlock = ^(int index, BOOL open) {
        if (index == 0) {
            
            // 设置占位图片
            if(open) {
                if(self.pushConfig.orientation == AlivcLivePushOrientationPortrait) {
                    self.pushConfig.pauseImg = [UIImage imageNamed:@"background_push.png"];
                } else{
                    self.pushConfig.pauseImg = [UIImage imageNamed:@"background_push_land.png"];
                }
            }else {
                self.pushConfig.pauseImg = nil;
            }
            
        } else {
            if(open) {
                if(self.pushConfig.orientation == AlivcLivePushOrientationPortrait) {
                    self.pushConfig.networkPoorImg = [UIImage imageNamed:@"poor_network.png"];
                } else{
                    self.pushConfig.networkPoorImg = [UIImage imageNamed:@"poor_network_land.png"];
                }
            }else {
                self.pushConfig.networkPoorImg = nil;
            }
        }
    };
    
    AlivcParamModel *qualityModeModel = [[AlivcParamModel alloc] init];
    qualityModeModel.title = NSLocalizedString(@"quality_mode_label", nil);
    qualityModeModel.segmentTitleArray = @[NSLocalizedString(@"push_high", nil),NSLocalizedString(@"push_fluency", nil),NSLocalizedString(@"push_custom", nil)];
    qualityModeModel.defaultValue = 0;
    qualityModeModel.reuseId = AlivcParamModelReuseCellSegment;
    qualityModeModel.segmentBlock = ^(int value) {
        self.pushConfig.qualityMode = value;
        [self updateBitrateAndFPSCell];
    };
    
    self.dataArray = [NSMutableArray arrayWithObjects:resolutionModel, qualityModeModel,targetBitrateModel, minBitrateModel, initBitrateModel, audioBitrateModel, audioSampelModel, fpsModel, minFPSModel, gopModel, reconnectDurationModel, reconnectTimeModel, orientationModel, audioChannelModel, audioProfileModel, mirrorModel, audiOnly_encodeModeModel, videoOnly_audioHardwareEncodeModel,backgroundImage_networkWeakImageModel,autoFocus_FlashModel, beauty_cameraTypeModel, autoBitrate_resolutionModel, asyncModel, watermarkModel, nil];
    
    // 默认支持背景图片和弱网图片推流
    self.pushConfig.pauseImg = [UIImage imageNamed:@"background_push.png"];
    self.pushConfig.networkPoorImg = [UIImage imageNamed:@"poor_network.png"];
}


- (void)updateBitrateAndFPSCell {
    
    int targetBitrate = 0;
    int minBitrate = 0;
    int initBitrate = 0;
    BOOL enable = NO;
    
    if (self.pushConfig.qualityMode == AlivcLivePushQualityModeFluencyFirst) {
        // 流畅度优先模式，bitrate 固定值不可修改
        enable = NO;
        switch (self.pushConfig.resolution) {
            case AlivcLivePushResolution180P:
                targetBitrate = 250;
                minBitrate = 80;
                initBitrate = 200;
                break;
            case AlivcLivePushResolution240P:
                targetBitrate = 350;
                minBitrate = 120;
                initBitrate = 300;
                break;
            case AlivcLivePushResolution360P:
                targetBitrate = 600;
                minBitrate = 200;
                initBitrate = 400;
                break;
            case AlivcLivePushResolution480P:
                targetBitrate = 800;
                minBitrate = 300;
                initBitrate = 600;
                break;
            case AlivcLivePushResolution540P:
                targetBitrate = 1000;
                minBitrate = 300;
                initBitrate = 800;
                break;
            case AlivcLivePushResolution720P:
                targetBitrate = 1200;
                minBitrate = 300;
                initBitrate = 1000;
                break;
            default:
                break;
        }
    }
    
    if (self.pushConfig.qualityMode == AlivcLivePushQualityModeResolutionFirst) {
        // 清晰度优先模式，bitrate 固定值不可修改
        enable = NO;
        switch (self.pushConfig.resolution) {
            case AlivcLivePushResolution180P:
                targetBitrate = 350;
                minBitrate = 120;
                initBitrate = 300;
                break;
            case AlivcLivePushResolution240P:
                targetBitrate = 550;
                minBitrate = 180;
                initBitrate = 450;
                break;
            case AlivcLivePushResolution360P:
                targetBitrate = 800;
                minBitrate = 300;
                initBitrate = 600;
                break;
            case AlivcLivePushResolution480P:
                targetBitrate = 1000;
                minBitrate = 300;
                initBitrate = 800;
                break;
            case AlivcLivePushResolution540P:
                targetBitrate = 1200;
                minBitrate = 600;
                initBitrate = 1000;
                break;
            case AlivcLivePushResolution720P:
                targetBitrate = 1800;
                minBitrate = 600;
                initBitrate = 1500;
                break;
            default:
                break;
        }
    }
    
    if (self.pushConfig.qualityMode == AlivcLivePushQualityModeCustom) {
        // 自定义模式，bitrate 固定值可修改
        enable = YES;
        targetBitrate = self.pushConfig.targetVideoBitrate;
        minBitrate = self.pushConfig.minVideoBitrate;
        initBitrate = self.pushConfig.initialVideoBitrate;
    }
    
    AlivcParamTableViewCell *targetCell = [self.paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    [targetCell updateDefaultValue:targetBitrate enable:enable];
    
    AlivcParamTableViewCell *minCell = [self.paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    [minCell updateDefaultValue:minBitrate enable:enable];
    
    AlivcParamTableViewCell *initCell = [self.paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
    [initCell updateDefaultValue:initBitrate enable:enable];
}

- (NSString *)getWatermarkPathWithIndex {
    
    NSString *watermarkBundlePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"watermark"] ofType:@"png"];
    
    return watermarkBundlePath;
}

#pragma mark - TableViewdelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AlivcParamModel *model = self.dataArray[indexPath.row];
    if (model) {
        NSString *cellIdentifier = [NSString stringWithFormat:@"AlivcLivePushTableViewIdentifier%ld", (long)indexPath.row];
        AlivcParamTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[AlivcParamTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell configureCellModel:model];
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    [self.view endEditing:YES];
    
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self.view endEditing:YES];
}


#pragma mark - Keyboard

- (void)addKeyboardNoti {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)keyboardWillShow:(NSNotification *)sender {
    
    if(!self.isKeyboardShow){
        
        //获取键盘的frame
        CGRect keyboardFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        // 修改tableView frame
        [UIView animateWithDuration:0 animations:^{
            CGRect frame = self.paramTableView.frame;
            frame.size.height = frame.size.height - keyboardFrame.size.height;
            self.paramTableView.frame = frame;
        }];
        
        self.isKeyboardShow = true;
    }
    
}


- (void)keyboardWillHide:(NSNotification *)sender {
    
    if(self.isKeyboardShow){
        self.paramTableView.frame = self.tableViewFrame;
        self.isKeyboardShow = false;
    }
    
}




#pragma mark - TO PublisherVC
- (void)publiherButtonAction:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AliyunNotificationEnterThePushVc" object:nil];
    
    // 更新水印坐标
    if (self.isUseWatermark) {
        NSString *watermarkPath = [self getWatermarkPathWithIndex];
        [self.pushConfig addWatermarkWithPath:watermarkPath
                              watermarkCoordX:0.8
                              watermarkCoordY:0.1
                               watermarkWidth:0.8];
    }
    if (self.imagPath) {
        [self.pushConfig addWatermarkWithPath:self.imagPath
                              watermarkCoordX:0.8
                              watermarkCoordY:0.3
                               watermarkWidth:0.8];
    }
    LivePusherViewController *publisherVC = [[LivePusherViewController alloc] init];
    publisherVC.pushURL = self.loginDic[@"pushUrl"];
    publisherVC.pushConfig = self.pushConfig;
    publisherVC.isUseAsyncInterface = self.isUseAsync;
    publisherVC.loginDic=self.loginDic;
    publisherVC.isUserMainStream = self.isUserMainStream;
    publisherVC.channelId=self.channelId;
    publisherVC.password=self.password;
    [self presentViewController:publisherVC animated:YES completion:nil];
    
}

- (UIImage *)snapshotWithView:(UIView *)view
{
    CGSize size = view.bounds.size;
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

-(NSString *)getImagePath:(UIImage *)Image {
    
    NSString * filePath = nil;
    NSData * data = nil;
    
    data = UIImagePNGRepresentation(Image);
    
    //图片保存的路径
    //这里将图片放在沙盒的documents文件夹中
    NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    //文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //把刚刚图片转换的data对象拷贝至沙盒中
    [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString * ImagePath = [[NSString alloc]initWithFormat:@"/ChannelInfo.png"];
    [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:ImagePath] contents:data attributes:nil];
    
    //得到选择后沙盒中图片的完整路径
    filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,ImagePath];
    
    return filePath;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (BOOL)shouldAutorotate
{
    return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}


@end

